package misscl.online.slh_gym.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtils {

    //过期时间--12小时
    private static long expire = 43200;
    //32位秘钥
    private static String secret="zxcvbnmasdfghjklqwertyuiop123456";

    //生成token
    public static String generateToken(String userId){

        Date now = new Date();
        Date expiration = new Date(now.getTime()+1000*expire);


        return Jwts.builder()
                //设置头部信息
                .setHeaderParam("type","JWT")
                //载荷信息
                .setSubject(userId)
                //生效时间
                .setIssuedAt(now)
                //过期时间
                .setExpiration(expiration)
                //签名算法
                .signWith(SignatureAlgorithm.HS512,secret)
                .compact();

    }


    public static Claims getClaimsByToken(String token){

        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();



    }


}
